// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   Home.java

package jones.behavior;

import jones.SimpleRobotInterface;
import jones.ValueNotAvailableException;

// Referenced classes of package jones.behavior:
//            Behavior, Arbiter

public class Home implements Behavior {

	private static final String NAME = "Home";
	private double gain = 0;
	private double speed = 0;
	private boolean isOriented = false;
	private boolean isHomeWhenPushing = false;

	public Home() {
		gain = 2D;
		speed = 255D;
		isOriented = false;
		isHomeWhenPushing = false;
	}

	public Home(double gain, double speed, boolean isHomeWhenPushing) {
		this.gain = 2D;
		this.speed = 255D;
		isOriented = false;
		this.isHomeWhenPushing = false;
		this.gain = gain;
		this.speed = speed;
		this.isHomeWhenPushing = isHomeWhenPushing;
	}

	public String getName() {
		return "Home";
	}

	public double getGain() {
		return gain;
	}

	public void setGain(double gain) {
		this.gain = gain;
	}

	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public void update(double d) {
	}

	public boolean isHomeWhenPushing() {
		return isHomeWhenPushing;
	}

	public void setHomeWhenPushing(boolean isHomeWhenPushing) {
		this.isHomeWhenPushing = isHomeWhenPushing;
	}

	public void act(Arbiter arbiter) {
		try {
			SimpleRobotInterface robot = (SimpleRobotInterface) arbiter
					.getRobotInterface(((Behavior) (this)));
			if (isOriented) {
				robot.setLeftWheelSpeed((int) speed);
				robot.setRightWheelSpeed((int) speed);
				isOriented = false;
				return;
			}
			if (isHomeWhenPushing && !robot.isPushing())
				return;
			if (robot.getLeftPhoto() == 0 && robot.getRightPhoto() == 0) {
				isOriented = false;
				return;
			}
			double leftPhoto = robot.getLeftPhoto();
			double rightPhoto = robot.getRightPhoto();
			double angularVelocity = gain * (leftPhoto - rightPhoto);
			robot.setLeftWheelSpeed((int) (-angularVelocity));
			robot.setRightWheelSpeed((int) angularVelocity);
			isOriented = true;
		} catch (ValueNotAvailableException ex) {
		}
	}

	public String toString() {
		return "Home";
	}
}
